#include <bits/stdc++.h>
#define int long long
using namespace std;

int n, m, T, totans, ans[25], a[100005], c[100005], X[5], lft[100005];

char d[25][100005];

signed main()
{
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i <= m; i++)
		cin >> c[i];
	int now = 1, k = 0;
	while (now < n)
	{
		now *= 2;
		k++;
	}
	now = 1;
	for (int i = 1; i <= k; i++)
	{
		now *= 2;
		for (int j = 1; j <= (1 << k) / now; j++)
			cin >> d[i][j];
	}
	cin >> T;
	while (T--)
	{
		for (int i = 0; i < 4; i++)
			cin >> X[i];
		for (int i = 1; i <= n; i++)
			a[i] = a[i] ^ X[i % 4];
		for (int i = 0; i <= k; i++)
		{
			for (int j = 1; j <= (1 << i); j++)
				lft[j] = j;
			now = 1;
			for (int j = 1; j <= i; j++)
			{
				now *= 2;
				for (int l = 1; l <= (1 << i) / now; l++)
				{
					if (d[j][l] == '0')
					{
						if (a[lft[2 * l - 1]] >= j) lft[l] = lft[2 * l - 1];
						else lft[l] = lft[2 * l];
					}
					else
					{
						if (a[lft[2 * l]] >= j) lft[l] = lft[2 * l];
						else lft[l] = lft[2 * l - 1];
					}
				}
			}
			ans[i] = lft[1];
		}
		totans = 0;
		for (int i = 1; i <= m; i++)
		{
			int x = log2(c[i]);
			totans ^= (i * ans[x]);
		}
		cout << totans << endl;
	}
	return 0;
}
